import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
from io import StringIO

mpl.rcParams['pdf.fonttype'] = 42
mpl.rcParams['ps.fonttype'] = 42
mpl.rcParams['axes.linewidth'] = 1.5
mpl.rcParams['lines.linewidth'] = 3
mpl.rcParams['lines.markersize'] = 10
mpl.rcParams['font.size'] = 15
mpl.rcParams['xtick.labelsize'] = 14
mpl.rcParams['ytick.labelsize'] = 16
mpl.rcParams['legend.fontsize'] = 14
mpl.rcParams['legend.framealpha'] = 0
mpl.rcParams['legend.borderpad'] = 0.1
mpl.rcParams["axes.labelweight"] = "bold"
mpl.rcParams["axes.labelsize"] = 14
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['figure.figsize'] = (4, 4)
# mpl.rc("font",family='MicroSoft YaHei',weight="bold")

df = pd.read_csv('py/alto.csv', sep=",")

times = [800 / int(row['Rate']) for _,row in df.iterrows()]
df['Time'] = times
print(df)

  
def plot_time():
  for bw in range(100, 160, 10):
    if bw != 130:
      continue
    plt.figure()
    d = df.loc[df['Bandwidth'] == bw]
    sns.lineplot(x = 'Delay',
                y = 'Time',
                hue = 'System',
                markers=True,
                style='System',
                data = d)
    if bw == 100 or bw == 130:
      plt.ylabel('Time (s)')
    else:
      plt.ylabel(None)
    plt.xlabel('S1-S2 延迟 (ms)', fontdict={'family': 'SimHei'})
    plt.legend().set_title(None)
    # plt.xticks([i * 10 for i in range(5,16)])
    # plt.xlim(50, 150)
    # plt.yscale('log')
    plt.tight_layout(pad=1)
    plt.savefig('./figures/nai/eval-alto-time%s.pdf' % bw)
    # Show the plot
    plt.show()
    # plt.close()

def stat():
  socket_time_all = df.loc[df['System'] == 'Socket']['Time'].tolist()
  socket_time = df.loc[df['System'] == 'Socket']['Time'].tolist()[5:]
  socker_time_all = df.loc[df['System'] == 'Socker']['Time'].tolist()
  socker_time = df.loc[df['System'] == 'Socker']['Time'].tolist()[5:]
  
  # ratio = [(i-j)/i for i,j in zip(baselines, fusionfixed)]
  # ratio = [(i-j)/i for i,j in zip(baselines, fusionvar)]
  # ratio = [(i-j)/i for i,j in zip(fusionps, fusionfixed)]
  # ratio = [(i-j)/i for i,j in zip(fusionps, fusionvar)]
  print((sum(socket_time_all) - sum(socker_time_all)) / sum(socket_time_all))
  # print((sum(socker_mem) - sum(socket_mem)) / sum(socket_mem))
  # print((sum(socket_time)) / sum(socker_time))

# stat()
plot_time()
# plot_size()
# plot_time_overhead()
# plot_mem_overhead()

  